#include"sort.h"
#include"Stack.h"
void Swap(int* a, int* b) {
	int tmp = *a;
	*a = *b;
	*b = tmp;
}
void Print(int* arr, int sz) {
	for (int i = 0; i < sz; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n");
}
void InsertSort(int* arr, int sz) {
	for (int i = 0; i < sz - 1; i++) {
		int end = i;
		int tmp = arr[end + 1];
		while (end >= 0) {
			if (arr[end] > tmp) {
				arr[end + 1] = arr[end];
				end--;
			}
			else break;
		}
		arr[end + 1] = tmp;
	}
}
void ShellSort(int* arr, int sz) {
	int gap = sz;
	while (gap > 1) {
		gap = gap / 3 + 1;
		for (int i = 0; i < sz - gap; i++) {
			int end = i;
			int tmp = arr[end + gap];
			while (end >= 0) {
				if (arr[end] > tmp) {
					arr[end + gap] = arr[end];
					end -= gap;
				}
				else break;
			}
			arr[end + gap] = tmp;
		}
	}
}
void SelectSort(int* arr, int sz) {
	int begin = 0, end = sz - 1;
	while (begin < end) {
		int mini = begin, maxi = begin;
		for (int i = begin; i <= end; i++) {
			while (arr[i] < arr[mini]) mini = i;
			while (arr[i] > arr[maxi]) maxi = i;
		}
		if (maxi == begin) maxi = mini;
		Swap(&arr[mini], &arr[begin]);
		Swap(&arr[maxi], &arr[end]);
		begin++, end--;
	}
}
void BubbleSort(int* arr, int sz) {
	for (int i = 0; i < sz; i++) {
		int flag = 0;
		for (int j = 0; j < sz - 1 - i; j++) {
			if (arr[j] > arr[j + 1]) Swap(&arr[j], &arr[j + 1]);
			flag = -1;
		}
		if (flag == 0) break;
	}
}
//int _QuickSort(int* arr, int left, int right) {
//	int keyi = left;
//	left++;
//	while (left <= right) {
//		while (left <= right && arr[left] < arr[keyi]) left++;
//		while (left <= right && arr[right] > arr[keyi]) right--;
//		if (left <= right) Swap(&arr[left++], &arr[right--]);
//		else break;
//	}
//	Swap(&arr[right], &arr[keyi]);
//	return right;
//}//霍尔版本
int _QuickSort1(int* arr, int left, int right)
{
	int hole = left;
	int key = arr[hole];
	//left++;

	while (left < right)
	{
		while (left < right && arr[right] >= key)
		{
			right--;
		}
		arr[hole] = arr[right];
		hole = right;
		while (left < right && arr[left] <= key)
		{
			left++;
		}
		arr[hole] = arr[left];
		hole = left;
	}
	arr[hole] = key;
	return hole;
}
int _QuickSort(int* arr, int left, int right) {
	int cur = left + 1;
	int prev = left;
	int keyi = left;
	while (cur <= right) {
		if (arr[cur] < arr[keyi] && ++prev != cur) {
			Swap(&arr[cur], &arr[prev]);
		}
		cur++;
	}
	Swap(&arr[keyi], &arr[prev]);
	return prev;
}//前后指针版本


void QuickSort(int* arr, int left, int right) {
	if (left >= right) return;
	int keyi = _QuickSort1(arr, left, right);
	QuickSort(arr, left, keyi - 1);
	QuickSort(arr, keyi + 1, right);
}
void QuickSortNonR(int* arr, int left, int right) {
	Stack st;
	StackInit(&st);
	StackPush(&st, left);
	StackPush(&st, right);
	while (!StackEmpty(&st)) {
		int end = StackTop(&st);
		StackPop(&st);
		int begin = StackTop(&st);
		StackPop(&st);

		int cur = begin + 1;
		int prev = begin;
		int key = begin;
		while (cur <= end) {
			if (arr[cur] < arr[key] && ++prev != cur) {
				Swap(&arr[cur], &arr[prev]);
			}
			cur++;
		}
		Swap(&arr[prev], &arr[key]);
		key = prev;

		if (key - 1 > begin) {
			StackPush(&st, begin);
			StackPush(&st, key - 1);
		}
		if (key + 1 < end) {
			StackPush(&st, key+1);
			StackPush(&st, end);
		}
	}

	StackDestroy(&st);

}

void _MergeSort(int* arr, int* tmp, int left, int right) {
	if (left >= right) return;
	int mid = (right + left) / 2;
	_MergeSort(arr, tmp, left, mid);
	_MergeSort(arr, tmp, mid + 1, right);

	int begin1 = left, end1 = mid;
	int begin2 = mid + 1, end2 = right;
	int index = begin1;
	while (begin1 <= end1 && begin2 <= end2) {
		if (arr[begin1] < arr[begin2]) {
			tmp[index++] = arr[begin1++];
		}
		else {
			tmp[index++] = arr[begin2++];
		}
	}
	while (begin1 <= end1) {
		tmp[index++] = arr[begin1++];
	}
	while (begin2 <= end2) {
		tmp[index++] = arr[begin2++];
	}
	for (int i = left; i <= right; i++) {
		arr[i] = tmp[i];
	}
}


void MergeSort(int* arr, int sz) {
	int* tmp = (int*)malloc(sizeof(int) * sz);
	_MergeSort(arr, tmp, 0, sz - 1);
	free(tmp);
}